package com.car.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.entity.RepairRecord;
import com.car.mapper.RepairRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 维修记录服务
 *
 * @author Car System
 */
@Service
@RequiredArgsConstructor
public class RepairRecordService {

    private final RepairRecordMapper repairRecordMapper;

    /**
     * 分页查询维修记录（带车辆和客户信息）
     */
    public IPage<RepairRecord> page(Integer current, Integer size, String keyword, 
                                   String status, String startTime, String endTime) {
        Page<RepairRecord> page = new Page<>(current, size);
        return repairRecordMapper.selectRecordPageWithDetails(page, keyword, status, startTime, endTime);
    }

    /**
     * 根据车辆ID查询维修记录
     */
    public List<RepairRecord> listByVehicleId(Long vehicleId) {
        LambdaQueryWrapper<RepairRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairRecord::getVehicleId, vehicleId)
                .orderByDesc(RepairRecord::getStartTime);
        return repairRecordMapper.selectList(wrapper);
    }

    /**
     * 根据客户ID查询维修记录
     */
    public List<RepairRecord> listByCustomerId(Long customerId) {
        LambdaQueryWrapper<RepairRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairRecord::getCustomerId, customerId)
                .orderByDesc(RepairRecord::getStartTime);
        return repairRecordMapper.selectList(wrapper);
    }

    /**
     * 根据ID查询维修记录
     */
    public RepairRecord getById(Long id) {
        return repairRecordMapper.selectById(id);
    }

    /**
     * 新增维修记录
     */
    public void save(RepairRecord record) {
        repairRecordMapper.insert(record);
    }

    /**
     * 更新维修记录
     */
    public void update(RepairRecord record) {
        repairRecordMapper.updateById(record);
    }

    /**
     * 更新维修状态
     */
    public void updateStatus(Long id, String status) {
        RepairRecord record = repairRecordMapper.selectById(id);
        if (record == null) {
            throw new RuntimeException("维修记录不存在");
        }
        
        record.setStatus(status);
        if ("completed".equals(status)) {
            record.setEndTime(LocalDateTime.now());
        }
        
        repairRecordMapper.updateById(record);
    }

    /**
     * 删除维修记录
     */
    public void delete(Long id) {
        repairRecordMapper.deleteById(id);
    }

    /**
     * 查询本月维修数量
     */
    public Long countThisMonth() {
        LocalDateTime startOfMonth = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        LambdaQueryWrapper<RepairRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(RepairRecord::getStartTime, startOfMonth);
        return repairRecordMapper.selectCount(wrapper);
    }

    /**
     * 查询本月维修费用
     */
    public BigDecimal sumCostThisMonth() {
        LocalDateTime startOfMonth = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        
        List<RepairRecord> records = repairRecordMapper.selectList(
                new LambdaQueryWrapper<RepairRecord>()
                        .ge(RepairRecord::getStartTime, startOfMonth)
        );
        
        return records.stream()
                .map(RepairRecord::getCost)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 查询月度统计数据
     */
    public List<Map<String, Object>> getMonthlyStatistics(Integer months) {
        return repairRecordMapper.selectMonthlyStatistics(months);
    }
}

